package com.cxz.kotlin.baselibs.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;



public class MultiViewHolder extends RecyclerView.ViewHolder{

    private SparseArray<View> views;
    private Context context;

    private View expandView;

    public void setExpandView(View expandView) {
        this.expandView = expandView;
    }

    private MultiViewHolder(Context context, View itemView) {
        super(itemView);
        this.context = context;
        views = new SparseArray<>();
    }



    /**
     * 取得一个RecyclerHolder对象
     * @param context 上下文
     * @param parent
     * @param itemView 子项
     * @return 返回一个RecyclerHolder对象
     */
    public static MultiViewHolder getRecyclerHolder(Context context, ViewGroup parent, int itemView){
        View view = LayoutInflater.from(context).inflate(itemView, parent, false);

        return new MultiViewHolder(context,view);
    }

    public SparseArray<View> getViews(){
        return this.views;
    }

    /**
     * 通过view的id获取对应的控件，如果没有则加入views中
     * @param viewId 控件的id
     * @return 返回一个控件
     */
    public <T extends View> T getView(int viewId){
        View view = views.get(viewId);
        if (view == null ){
            view = itemView.findViewById(viewId);
            views.put(viewId,view);
        }
        return (T) view;
    }

    /**
     * 设置字符串
     */
    public MultiViewHolder setBtnText(int viewId, String text){
        Button tv = getView(viewId);
        tv.setText(text);
        return this;
    }
    /**
     * 设置editText监听
     */
    public MultiViewHolder setEtListenerOnContentClear(int clearId, int etId){

        final EditText etContent = getView(etId);
        final View clear = getView(clearId);
        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etContent.setText("");
            }
        });
        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                clear.setVisibility(etContent.getText().toString().length() > 0 ? View.VISIBLE : View.GONE);
            }
        });
        return this;
    }
    /**
     * 设置editText监听
     */
    public MultiViewHolder setEtListenerClearBtnState(int clearId, final int btnid, int etId){

        final EditText etContent = getView(etId);
        final View clear = getView(clearId);
        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etContent.setText("");
            }
        });
        etContent.addTextChangedListener(new TextWatcher() {

            private Button button;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                clear.setVisibility(etContent.getText().toString().length() > 0 ? View.VISIBLE : View.GONE);

                if (null == button) {
                    button = getView(btnid);
                }
                button.setEnabled(etContent.getText().toString().length() > 0 ? true : false);
            }
        });
        return this;
    }
    /**
     * 设置字符串
     */
    public MultiViewHolder setHint(int viewId, String text){
        EditText tv = getView(viewId);
        tv.setHint(text);
        return this;
    }

    /**
     * 设置字符串
     */
    public MultiViewHolder setText(int viewId, String text){
        TextView tv = getView(viewId);
        tv.setText(text);
        return this;
    }

    /**
     * 设置图片
     */
    public MultiViewHolder setImageResource(int viewId, int drawableId){
        ImageView iv = getView(viewId);
        iv.setImageResource(drawableId);
        return this;
    }

    /**
     * 设置图片
     */
    public MultiViewHolder setImageBitmap(int viewId, Bitmap bitmap){
        ImageView iv = getView(viewId);
        iv.setImageBitmap(bitmap);
        return this;
    }




}
